package main;

//import java.util.*;
import java.awt.Cursor;
import java.io.*;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.EmbeddedGraphDatabase;

import programOutput.ParsingOutput;

import audit.AuditWriter;

import configuration.UserFields_XmlReader;
import configuration.UserFields_XmlWriter;
import db_neo4j.EmbeddedNeo4j;
import db_neo4j.Query_DB;

import recordProcessing.RecordCollection;
import recordProcessing.RecordParser;
import table.TableCollection;
import userInterface.GUIController;
import userInterface.MainPage;
import fields.FieldMatcher;
import fileProcessing.*;

public class Main {	
	public static long begin = System.currentTimeMillis();
	public static boolean usingDB = false;
	//Singleton utility classes
	public static EmbeddedNeo4j db;
	public static AuditWriter audit;
	public static RecordCollection rCollection;
	public static TableCollection tCollection;
	/**
	 * This is the main method to run the complete parsing process, to include directory creation,
	 * algorithm execution, and data management.
	 * @param args
	 * @throws IOException
	 * @throws ParsingException 
	 */
	public Main(File inFile,Boolean send) throws ParsingException{
		try {
			//build project directories
			new ProjectDirectories().buildProjectDirs();
			
			//initialize singletons
			if(send){
				usingDB = true;
				db = new EmbeddedNeo4j();
				db.openDB();
			}
				
			
			audit = AuditWriter.getAuditWriter();
			tCollection = TableCollection.getTableHashClass();
			rCollection = RecordCollection.getRecordHashClass();
			
			//preprocess PII file specified by user
			FilePreProcessor preProcessingSession = new FilePreProcessor(inFile);
			preProcessingSession.processFile();
			
			//Parse all sources from PII file for records.			
			new RecordParser(preProcessingSession.preProcessing);
			
			//Output CSV files
			new ParsingOutput(",",true,false);
			
			//Close auditFile
			closeAuditFile();
			
			//Close Database
			if(send){
				db.shutDown();
			}
			//Program closes when user clicks exit on GUI
			
		} catch (ParsingException e) {
			GUIController.getMainPage().setStatus("Exception Caught in main, see audit file for details.\n");
			GUIController.getMainPage().parseFile.setEnabled(true);
			GUIController.getMainPage().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			System.out.println(e.getMessage());
//			System.out.println("Exception Caught in main, see audit file for details.");
			audit.writeToAudit(e.getMessage());
			closeAuditFile();
		} catch (FileNotFoundException e) {
			GUIController.getMainPage().setStatus("Exception Caught in main, see audit file for details.\n");
			GUIController.getMainPage().parseFile.setEnabled(true);
			GUIController.getMainPage().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
//			System.out.println("Exception Caught in main, see audit file for details.");
			audit.writeToAudit(e.getMessage());
			closeAuditFile();
		}
	}
	
	private void closeAuditFile() throws ParsingException{
		audit.writeExecutionTime("Final execution time: ");
		audit.closeAuditFile();
	}
  
} // end of program
